package org.baicaix.utils {

	public class StringUtils {

		/**
		 * Returns value is a string type value.
		 * with undefined or null value, false returned.
		 */
		public static function isString(value : *) : Boolean {
			return value is String;
		}

		public static function castString(str : *) : String {
			return str as String;
		}

		/**
		 * replace oldString with newString in targetString
		 */
		public static function replace(targetString : String , oldString : String , newString : String) : String {
			return targetString.split(oldString).join(newString);
		}

		/**
		 * remove the blankspaces of left and right in targetString
		 */
		public static function trim(targetString : String) : String {
			return trimLeft(trimRight(targetString));
		}

		/**
		 * remove only the blankspace on targetString's left
		 */
		public static function trimLeft(targetString : String) : String {
			var tempIndex : int = 0;
			var tempChar : String = "";
			for(var i : int = 0 ;i < targetString.length ;i++) {
				tempChar = targetString.charAt(i);
				if(tempChar != " ") {
					tempIndex = i;
					break;
				}
			}
			return targetString.substr(tempIndex);
		}

		/**
		 * remove only the blankspace on targetString's right
		 */
		public static function trimRight(targetString : String) : String {
			var tempIndex : int = targetString.length - 1;
			var tempChar : String = "";
			for(var i : int = targetString.length - 1 ;i >= 0 ;i--) {
				tempChar = targetString.charAt(i);
				if(tempChar != " ") {
					tempIndex = i;
					break;
				}
			}
			return targetString.substring(0, tempIndex + 1);
		}

		public static function getCharsArray(targetString : String , hasBlankSpace : Boolean) : Array {
			var tempString : String = targetString;
			if(hasBlankSpace == false) {
				tempString = trim(targetString);
			} 		
			return tempString.split("");
		}

		public static function startsWith(targetString : String, subString : String) : Boolean {
			return (targetString.indexOf(subString) == 0);	
		}

		public static function endsWith(targetString : String, subString : String) : Boolean {
			return (targetString.lastIndexOf(subString) == (targetString.length - subString.length));	
		}

		public static function isLetter(chars : String) : Boolean {
			if(chars == null || chars == "") {
				return false;
			}
			for(var i : int = 0;i < chars.length;i++) {
				var code : uint = chars.charCodeAt(i);
				if(code < 65 || code > 122 || (code > 90 && code < 97)) {
					return false;
				}
			}
			return true;
		}

		/**
		 * 删除左部空白   
		 * tab,换行,水平制表符,回车,空格, (\t,\n,\f,\r,' ')(9,10,12,13,32,160,8203,8288,12288....)
		 * 
		 * @param	input	<b>	String	</b>
		 * @return
		 */
		static public function leftTrim(input : String) : String {
			// input 为 null | "" 时,返回自身
			if (!input) return input;
 
			// 第一个字符不是空白
			if (input.charCodeAt(0) > 32) return input;
 
			// 删除左部空白
			return input.replace(/^\s*/, "");
		}

		/**
		 * 删除右部空白 
		 * tab,换行,水平制表符,回车,空格, (\t,\n,\f,\r,' ')(9,10,12,13,32,160,8203,8288,12288....)
		 * 
		 * @param	input	<b>	String	</b>
		 * @return
		 */
		static public function rightTrim(input : String) : String {
			// input 为 null | "" 时,返回自身
			if (!input) return input;
 
			// 第一个字符不是空白
			if (input.charCodeAt(input.length - 1) > 32) return input;
 
			// 删除左部空白
			return input.replace(/\s*$/, "");
		}

		/**
		 * 清除字符串的所有空白  
		 * tab,换行,水平制表符,回车,空格, (\t,\n,\f,\r,' ')(9,10,12,13,32,160,8203,8288,12288....)
		 * 
		 * @param	input	<b>	String	</b> 要清除空白的字符串
		 * @return
		 */
		static public function removeWhitespace(input : String) : String {
			if(input)
				return input.replace(/\s/g, "");
			return input;
		}

		/**
		 * 生成 由 count 个 input 组成的字符串
		 * 
		 * @param	input	<b>	String	</b> intput 为 null | "" 时,返回 input
		 * @param	count	<b>	int		</b> count >= 0, 如果count < 0 ,则count = 0;
		 * @return
		 */
		static public function memset(input : String, count : int) : String {
			if (!input) return str;			// (null || "") 返回本身   
			var str : String = "";
			if (count < 0) count = 0;		// 防止溢出
			while (count--) {
				str += input;
			}
			return str;
		}
	}
}